package ControlLayer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import DBLayer.DbInPartOrder;
import DBLayer.DbIncomingOrder;
import DBLayer.DbOutPartOrder;
import DBLayer.DbOutgoingOrder;
import ModelLayer.*;

public class OrderCtr {
	
	DbIncomingOrder dbInO;
	DbOutgoingOrder dbOutO;
	DbInPartOrder dbInPO;
	DbOutPartOrder dbOutPO;
	CompanyCtr cCtr;
	ProductCtr pCtr;
	
	public OrderCtr()
	{
		//Constructor, initializes the db-objects and controller-obejcts
		dbInO = new DbIncomingOrder();
		dbOutO = new DbOutgoingOrder();
		dbInPO = new DbInPartOrder();
		dbOutPO = new DbOutPartOrder();
		cCtr = new CompanyCtr();
		pCtr = new ProductCtr();
	}
	
	public IncomingOrder getOrder(String orderId)
	{
		//Finds an order by using the orderId
		IncomingOrder iO = dbInO.findOrder(orderId);
		iO.setPartList(dbInPO.getAllPartOrders(orderId));
		iO.setC(cCtr.getCustomer1("0000000"+iO.getC().getCustomerId()));
		return iO;
	}
	
	public OutgoingOrder createOutgoingOrder()
	{
		//Creates a new outgoingOrder
		OutgoingOrder oO = new OutgoingOrder();
		Customer c = cCtr.getCustomer(00000001);
		oO.setC(c);		
		ArrayList<OutPartOrder> partList = new ArrayList<OutPartOrder>();
		oO.setPartList(partList);
		return oO;
	}
	
	public OutgoingOrder addOutPartOrder(int proId, int amount, OutgoingOrder oO)
	{
		//adds a partOrdet to the OutgoingOrder
		OutPartOrder oPartOrder = new OutPartOrder();
		
		oPartOrder.setP(pCtr.getProduct(proId));
		oPartOrder.setProductId(proId);
		oPartOrder.setAmount(amount);
		oO.getPartList().add(oPartOrder);
		
		return oO;
	}
	
	public void saveOutgoingOrder(OutgoingOrder oO)
	{
		//Saves the order object in the database
		int orderId = 0;
		try {
			orderId = dbOutO.insertOrder(oO);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int index = 0;
		while(index < oO.getPartList().size())
		{
			OutPartOrder pO = oO.getPartList().get(index);
			try {
				dbOutPO.insertPartOrder(pO, orderId);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			index++;
		}
	}
	
	public void addSupplierOutgoingOrder(OutgoingOrder oO, int supplierId)
	{
		//adds an supplier to the outgoingOrder
		Supplier s = cCtr.getSupplier(supplierId);
		oO.setS(s);
	}
	

	
	public ArrayList<IncomingOrder> getAllIncomingOrders(int whatOrder)
	{
		//Returns a list with incomingOrders by using several if-statements.
		ArrayList<IncomingOrder> incomingOrderList = dbInO.getAllOrders();
		ArrayList<IncomingOrder> resultOrderList = new ArrayList<IncomingOrder>();
		
		int index = 0;
		
		int checker = 0;

		if(whatOrder == 1)
		{
			while(index<incomingOrderList.size())
			{									
				String check = incomingOrderList.get(index).getOrderStatus();
				
				if(check == null)
				{
					resultOrderList.add(incomingOrderList.get(index));
				}
				
				index++;
			}
		}
		else if(whatOrder == 2)
		{
			while(index<incomingOrderList.size())
			{
				String check = incomingOrderList.get(index).getOrderStatus();
				if("Order opened".equals(check) || "Order packed".equals(check) || "Order needing products".equals(check))
				{
					resultOrderList.add(incomingOrderList.get(index));
				}
				index++;
			}
		}
		else
		{
			while(index<incomingOrderList.size())
			{
				String check = incomingOrderList.get(index).getOrderStatus();
				
				if("Order sent".equals(check))
				{
					resultOrderList.add(incomingOrderList.get(index));
				}
				index++;
			}
		}
		
		return resultOrderList;
	}
	
	public ArrayList<OutgoingOrder> getAllOutgoingOrders()
	{
		//returns all outgoingOrders as a list
		return dbOutO.getAllOrders();
	}
	
	public void orderStatus(int orderStatus, String orderId)
	{
		//Changes the orderstatus of an object
		IncomingOrder iO = getOrder(orderId);
		String status = null;
		String sent = null;
		
		if(orderStatus==1)
		{
			status = "Order opened";
		}
		else if(orderStatus==2)
		{
			status = "Order needing products";
		}
		else if(orderStatus==3)
		{
			status = "Order packed";
		}
		else if(orderStatus==4)
		{
			DateFormat df = new SimpleDateFormat("ddMMyyyy");
			status = "Order sent";
			Date today = Calendar.getInstance().getTime();
			sent = df.format(today);
			iO.setDateSent(sent);
		}
		
		iO.setOrderStatus(status);
		dbInO.updateOrder(iO, orderId);
	}
}